//
//  GPChatController+ChatToolView.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/19.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPChatController+ChatToolView.h"
#import "GPChatController+Message.h"
#import "ZZPhotoKit.h"
#import "GPVideoCallManager.h"
#import "GPMessageItem.h"
#import "RecorderManager.h"
#import "NSTimer+Extension.h"

@implementation GPChatController (ChatToolView)


#pragma mark - GPVoiceViewDelegate

- (void)voiceView:(GPVoiceView *)voiceView startRecordVoice:(UIButton *)button
{
    //开始录音
    NSLog(@"----->开始录音");
    //判断是否有录音权限
    if (![[RecorderManager sharedManager] isRecordAvailabe]) {
//        [KLProgressHUD showPromptHUDWithMessage:@"请在设置中添加录音权限"];
        return;
    }
    
    [RecorderManager sharedManager].delegate = self;
    [[RecorderManager sharedManager] startRecording];
    
    //创建计时器
    WEAKSELF
    weakSelf.voiceLength = 1;
    self.voiceTimer = [NSTimer kscheduledTimerWithTimeInterval:1.0 block:^{
        weakSelf.voiceLength++;
        [voiceView updateVoiceTimesecond:weakSelf.voiceLength];
    
    } repeats:YES];
}

- (void)voiceView:(GPVoiceView *)voiceView endRecordVoice:(UIButton *)button
{
    //结束录音
    NSLog(@"---->结束录音");
    [[RecorderManager sharedManager] stopRecording];
    
    if (self.voiceTimer) {
        [self.voiceTimer invalidate];
        self.voiceTimer = nil;
        self.voiceLength = 0;
    }
}

- (void)voiceView:(GPVoiceView *)voiceView cancelRecordVoice:(UIButton *)button
{
    //取消录音
    NSLog(@"---->取消录音");
    [[RecorderManager sharedManager] cancelRecording];
    
    if (self.voiceTimer) {
        [self.voiceTimer invalidate];
        self.voiceTimer = nil;
        self.voiceLength = 0;
    }
}


#pragma mark - GPChatToolViewDelegate
/**工具栏上的文本框内容改变*/
- (void)toolView:(GPChatToolView *)toolView editChanged:(UITextView *)textView
{
    
    //textView.contentSize.height > 30 && textView.contentSize.height < 100
//    if (textView.contentSize.height < 100) {
//        CGFloat height = textView.contentSize.height;
//        if (height < 35) {
//            height = 35;
//        }
//        [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
////            make.height.mas_equalTo(textView.contentSize.height + 10);
//            make.height.mas_equalTo(height + 10);
//        }];
//        [self.view layoutIfNeeded];
//    }
//    
//    //文本框滚动到最末
//    [textView setContentOffset:CGPointMake(0, textView.contentSize.height - textView.bounds.size.height) animated:NO];
}

/**工具栏上的文本框内容将要被编辑*/
- (void)toolView:(GPChatToolView *)toolView willBeginEditing:(UITextView *)textView
{
    // // // // GPLog(@">>>>>>>>>>>smallyou:文本框内容将要被编辑");
}

/**系统键盘上的回车按钮被点击*/
- (void)toolView:(GPChatToolView *)toolView didReturnKeyPressed:(UITextView *)textView
{
    // // // // // GPLog(@">>>>>>>>>>>smallyou:点击回车按钮");
    //发送文本消息
    [self operaionOfSendTextMessage:[self emotionText]];
}

/**工具栏上的表情按钮被点击*/
- (void)toolView:(GPChatToolView *)toolView didEmojiButtonClicked:(UIButton *)button
{
    // // // // // GPLog(@">>>>>>>>>>>smallyou:表情按钮被点击");
    
    //处理其他键盘
    if (!self.isEmojiViewShowing) {
        //关闭其他键盘--此处只需要关闭系统键盘即可
        [self systemKeyboardDisplay:NO animated:NO keyboardHeight:kEmojiViewHeight duration:kAnimationDuration];
        [self photosViewDisplay:NO aimated:NO];
        [self voiceViewDisplay:NO aimated:NO];
        [self.view endEditing:YES];
    }
    
    //显示emojiView
    [self emojiViewDisplay:!self.isEmojiViewShowing animated:NO];
}

/**工具栏上的发送按钮被点击*/
- (void)toolView:(GPChatToolView *)toolView didSendButtonClicked:(UIButton *)button
{
    // // // // // GPLog(@">>>>>>>>>>>smallyou:发送按钮被点击");
    NSString *text = [self emotionText];
    if (text.length == 0) {
        return;
    }

    //发送文本消息
    [self operaionOfSendTextMessage:text];

}

//语音聊天
- (void)toolView:(GPChatToolView *)toolView didVoiceButtonClicked:(UIButton *)button
{
    
    //处理其他键盘
    if (!self.isVoiceViewShowing) {
        //关闭其他键盘--此处只需要关闭系统键盘即可
        [self systemKeyboardDisplay:NO animated:NO keyboardHeight:kEmojiViewHeight duration:kAnimationDuration];
        [self emojiViewDisplay:NO animated:NO];
        [self photosViewDisplay:NO aimated:NO];
        [self.view endEditing:YES];
    }
    
    //显示photosView
    [self voiceViewDisplay:!self.isVoiceViewShowing aimated:NO];
    
}

//选择相册
- (void)toolView:(GPChatToolView *)toolView didPhotosButtonClicked:(UIButton *)button
{
    
    if(![AppGlobal isAssetsLibraryAvalable]){
        return;
    }
    
    //处理其他键盘
    if (!self.isPhotosViewShowing) {
        //关闭其他键盘--此处只需要关闭系统键盘即可
        [self systemKeyboardDisplay:NO animated:NO keyboardHeight:kEmojiViewHeight duration:kAnimationDuration];
        [self emojiViewDisplay:NO animated:NO];
        [self voiceViewDisplay:NO aimated:NO];
        [self.view endEditing:YES];
    }
    
    //显示photosView
    [self photosViewDisplay:!self.isPhotosViewShowing aimated:NO];
}

//相机拍照
- (void)toolView:(GPChatToolView *)toolView didCarmeraButtonClicked:(UIButton *)button
{
    if(![AppGlobal isCameraDeviceAvalable]){
        return;
    }
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = NO;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

//视频通话
- (void)toolView:(GPChatToolView *)toolView didVideoButtonClicked:(UIButton *)button
{
    if(![AppGlobal isCameraDeviceAvalable]){
        return;
    }
    
    NSString *username = self.chatViewModel.toId;
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [vc addAction: [UIAlertAction actionWithTitle:@"Voice Call" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[GPVideoCallManager shareManager] startCall:EMCallTypeVoice username:username ext:nil];
    }]];
    [vc addAction: [UIAlertAction actionWithTitle:@"Video Call" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[GPVideoCallManager shareManager] startCall:EMCallTypeVideo username:username ext:nil];
    }]];
    [vc addAction: [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:vc animated:YES completion:nil];
    
    
    
}

//地理位置
- (void)toolView:(GPChatToolView *)toolView didLocationButtonClicked:(UIButton *)button
{
//    [self operationOfSendLocationMessage:39.910098 longitude:116.404081 address:@"中国，北京市天安门广场大街道199号，中华人民的首都"];
//    GPMapController *vc = [[GPMapController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];

    GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:nil];
    GMSPlacePickerViewController *placePicker =
    [[GMSPlacePickerViewController alloc] initWithConfig:config];
    placePicker.delegate = self;
    
    [self presentViewController:placePicker animated:YES completion:nil];
    
    
}


#pragma mark - UIImagePickerControllerDelegate
/**拍照*/
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
    WEAKSELF
    [self dismissViewControllerAnimated:YES completion:^{
        // 1.获取并压缩用户拍摄的图片
        UIImage *selectedImg = info[UIImagePickerControllerOriginalImage];
        NSData *imageData = UIImageJPEGRepresentation(selectedImg, 0.3); // 图片分辨率降低
        selectedImg = [UIImage imageWithData: imageData];
        
        // 2.发送图片
        [weakSelf operationOfSendImageMessage:selectedImg];
        
    }];
}





#pragma mark - GPEmojiKeyboardDelegate
/**表情键盘上的发送按钮被点击*/
- (void)emojiKeyboardDidSendBtnClicked:(GPEmojiKeyboard *)emojiKeyboard
{
    // // // // // GPLog(@">>>>>>>>>>>smallyou:表情键盘发送按钮");
    NSString *text = [self emotionText];
    if (text.length == 0) {
        return;
    }
    
    //发送文本消息
    [self operaionOfSendTextMessage:text];
}

/**表情键盘上的普通表情被点击*/
- (void)emojiKeyboard:(GPEmojiKeyboard *)emojiKeyboard didNormalEmotionSelected:(GPTextAttachment *)attachment
{
    //1 更新textView的内容
    NSRange range = self.toolView.textView.selectedRange;
    
    //--- 设置textView的文字
    NSMutableAttributedString *textAttr = [[NSMutableAttributedString alloc] initWithAttributedString:self.toolView.textView.attributedText];
    
    NSAttributedString *imageAttr = [NSMutableAttributedString attributedStringWithAttachment:attachment];
    
    [textAttr replaceCharactersInRange:self.toolView.textView.selectedRange withAttributedString:imageAttr];
    UIFont *font = self.toolView.textView.font?self.toolView.textView.font:[UIFont systemFontOfSize:14.0];
    [textAttr addAttributes:@{NSFontAttributeName : font} range:NSMakeRange(self.toolView.textView.selectedRange.location, 1)];
    
    self.toolView.textView.attributedText = textAttr;
    
    //--- 会在textView后面插入空的,触发textView文字改变
    [self.toolView.textView insertText:@""];
    
    //--- 设置光标位置
    self.toolView.textView.selectedRange = NSMakeRange(range.location + 1, 0);
    
    //2 更新textView的尺寸
    [self toolView:self.toolView editChanged:self.toolView.textView];
    
}

/**表情键盘上的删除表情被点击*/
- (void)emojiKeyboardDidDeleteEmotionClicked:(GPEmojiKeyboard *)emojiKeyboard
{
    //1 更新textView的内容
    [self.toolView.textView deleteBackward];
    
    //2 更新尺寸
    [self toolView:self.toolView editChanged:self.toolView.textView];
}


#pragma mark - GPPhotosViewDelegate
//点击相册
- (void)photosViewDidClickAblumnButton:(GPPhotosView *)photosView
{
    [self photosViewDisplay:NO aimated:YES];
    
    //打开相册
    ZZPhotoController *photoController = [[ZZPhotoController alloc] init];
    photoController.selectPhotoOfMax = 9; // 设置最大选择张数
    WEAKSELF
    [photoController showIn:self result:^(id responseObject){
        //返回结果集
        NSArray <ZZPhoto *>*photoArray = (NSArray *)responseObject;
        for (ZZPhoto *photo in photoArray) {
            [weakSelf operationOfSendImageMessage:photo.originImage];
        }
    }];
}

//发送图片
- (void)photosView:(GPPhotosView *)photosView didSendImages:(NSArray<GPPhotosModel *> *)photos
{
    for (GPPhotosModel *photo in photos) {
        [self operationOfSendImageMessage:photo.image];

    }
}


#pragma mark - 工具方法
/**控制器表情键盘的显示*/
- (void)emojiViewDisplay:(BOOL)isDisplay animated:(BOOL)isAnimated
{
    //更新表情键盘的属性
    self.isEmojiViewShowing = isDisplay;
    
    //更新emojiView的约束
    CGFloat emojiOffset = isDisplay? -kEmojiViewHeight:0;
    [self.emojiView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_bottom).offset(emojiOffset);
    }];
    
    //更新toolView的约束(上下移动)
    CGFloat toolOffset = isDisplay?-kEmojiViewHeight:0;
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(toolOffset);
    }];
    
    //重新布局
    if (isAnimated) {
        [UIView animateWithDuration:kAnimationDuration animations:^{
            [self.view layoutIfNeeded];
        }];
    }else{
        [self.view layoutIfNeeded];
    }
    
    //滚动tableView
    if (isDisplay) {
        [self gp_scrollTableViewToBottom:YES];
    }
}

/**控制相册键盘的显示*/
- (void)photosViewDisplay:(BOOL)isDisplay aimated:(BOOL)isAnimated
{
    if (isDisplay) {
        [self.photosView show];
    }else{
        [self.photosView hide];
    }
    
    //更新相册键盘的属性
    self.isPhotosViewShowing = isDisplay;
    
    //更新photosView的约束
    CGFloat photosOffset = isDisplay?-kPhotosViewHeight:0;
    [self.photosView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_bottom).offset(photosOffset);
    }];
    
    //更新toolView的约束(上下移动)
    CGFloat toolOffset = isDisplay?-kPhotosViewHeight:0;
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(toolOffset);
    }];
    
    //重新布局
    if (isAnimated) {
        [UIView animateWithDuration:kAnimationDuration animations:^{
            [self.view layoutIfNeeded];
        }];
    }else{
        [self.view layoutIfNeeded];
    }
    
    //移动tableView
    if (isDisplay) {
        [self gp_scrollTableViewToBottom:YES];
    }
    
}

/**控制语音键盘的显示*/
- (void)voiceViewDisplay:(BOOL)isDisplay aimated:(BOOL)isAnimated
{
    //更新语音键盘的属性
    self.isVoiceViewShowing = isDisplay;
    
    //更新voiceView的约束
    CGFloat voiceOffset = isDisplay? -kVoiceViewHeight:0;
    [self.voiceView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_bottom).offset(voiceOffset);
    }];
    
    //更新toolView的约束(上下移动)
    CGFloat toolOffset = isDisplay?-kVoiceViewHeight:0;
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(toolOffset);
    }];
    
    //重新布局
    if (isAnimated) {
        [UIView animateWithDuration:kAnimationDuration animations:^{
            [self.view layoutIfNeeded];
        }];
    }else{
        [self.view layoutIfNeeded];
    }
    
    //滚动tableView
    if (isDisplay) {
        [self gp_scrollTableViewToBottom:YES];
    }
}


/**控制器系统键盘的显示*/
- (void)systemKeyboardDisplay:(BOOL)isDisplay animated:(BOOL)isAnimated keyboardHeight:(CGFloat)height duration:(CGFloat)duration
{
    //更新系统键盘的属性
    self.isSystemKeyboradShowing = isDisplay;
    
    //更新toolView的约束
    CGFloat toolOffset = isDisplay?-height:0;
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(toolOffset);
    }];
    
    //重新布局
    if (isAnimated) {
        [UIView animateWithDuration:duration animations:^{
            [self.view layoutIfNeeded];
        }];
    }else{
        [self.view layoutIfNeeded];
    }
    
    //滚动tableView
    if (isDisplay) {
        [self gp_scrollTableViewToBottom:YES];
    }

}

/**获取表情字符串*/
- (NSString *)emotionText
{
    
    NSMutableString *strM = [NSMutableString string];
    
    [self.toolView.textView.attributedText enumerateAttributesInRange:NSMakeRange(0, self.toolView.textView.attributedText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(NSDictionary<NSString *,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
        NSString *str = nil;
        
        GPTextAttachment *attachment = attrs[@"NSAttachment"];
        
        if (attachment) { // 表情
            str = attachment.emotionStr;
            [strM appendString:str];
        } else { // 文字
            str = [self.toolView.textView.attributedText.string substringWithRange:range];
            [strM appendString:str];
        }
        
    }];
    return strM;
}

/**发送文本消息操作*/
- (void)operaionOfSendTextMessage:(NSString *)text
{
    //发送文本消息
    WEAKSELF
    GPMessageItem *messageItem = [self sendTextMessage:text to:self.chatViewModel.toId conversationId:self.chatViewModel.conversationId chatType:self.chatViewModel.chatType progress:nil completion:^(GPMessageItem *messageItem,EMMessage *message, EMError *error) {
        NSLog(@">>>>>>>>>>smallyou:messsageItem=%zd,,,,message=%zd",messageItem.message.status,message.status);
        //不管成功与否，刷新表格（消息的发送状态SDK已经自动更新）
        [weakSelf.tableView reloadData];
    }];
    
    
    //更新数据源
    [self messageArray:self.messages addMessage:messageItem];
    [self.tableView reloadData];
    [self gp_scrollTableViewToBottom:NO];
    if (self.messages.count != 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
    
    //清空输入框
    self.toolView.textView.attributedText = nil;
    //恢复输入框的高度
    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(90 + 10);
    }];
    [self.view layoutIfNeeded];
}

/**发送图片消息操作*/
- (void)operationOfSendImageMessage:(UIImage *)image
{
    //发送图片消息
    WEAKSELF
    GPMessageItem *messageItem = [self sendImageMessage:image thumbnailImage:nil to:self.chatViewModel.toId conversationId:self.chatViewModel.conversationId chatType:self.chatViewModel.chatType progress:nil completion:^(GPMessageItem *messageItem, EMMessage *message, EMError *error) {
        NSLog(@">>>>>>>>>>smallyou:messsageItem=%zd,,,,message=%zd",messageItem.message.status,message.status);
        //不管成功与否，刷新表格（消息的发送状态SDK已经自动更新）
        [weakSelf.tableView reloadData];
    }];
    
    //更新数据源
    [self messageArray:self.messages addMessage:messageItem];
    [self.tableView reloadData];
    [self gp_scrollTableViewToBottom:NO];
    if (self.messages.count != 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }

    [self.view layoutIfNeeded];

}

/**发送语音消息操作*/
- (void)operationOfSendVoiceMessage:(NSString *)filePath fileName:(NSString *)fileName duration:(int)duration
{
    //发送语音消息
    WEAKSELF
    GPMessageItem *messageItem = [self sendVoiceMessage:filePath displayName:fileName duration:duration conversationId:self.chatViewModel.conversationId to:self.chatViewModel.toId chatType:self.chatViewModel.chatType progress:nil completion:^(GPMessageItem *messageItem, EMMessage *message, EMError *error) {
        NSLog(@">>>>>>>>>>smallyou:messsageItem=%zd,,,,message=%zd",messageItem.message.status,message.status);
        //不管成功与否，刷新表格（消息的发送状态SDK已经自动更新）
        [weakSelf.tableView reloadData];
    }];
    
    //更新数据源
    [self messageArray:self.messages addMessage:messageItem];
    [self.tableView reloadData];
    [self gp_scrollTableViewToBottom:NO];
    if (self.messages.count != 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
    
    [self.view layoutIfNeeded];
}


/**发送地理位置的操作*/
- (void)operationOfSendLocationMessage:(double)latitude longitude:(double)longitude address:(NSString *)address name:(NSString *)name
{
    //发送地理位置消息
    WEAKSELF
    GPMessageItem *messageItem = [self sendLocationMessageLatitude:latitude longitude:longitude address:address name:name to:self.chatViewModel.toId conversationId:self.chatViewModel.conversationId chatType:self.chatViewModel.chatType progress:nil completion:^(GPMessageItem *messageItem, EMMessage *message, EMError *error) {
        NSLog(@">>>>>>>>>>smallyou:messsageItem=%zd,,,,message=%zd",messageItem.message.status,message.status);
        //不管成功与否，刷新表格（消息的发送状态SDK已经自动更新）
        [weakSelf.tableView reloadData];
    }];
    
    //更新数据源
    [self messageArray:self.messages addMessage:messageItem];
    [self.tableView reloadData];
    [self gp_scrollTableViewToBottom:NO];
    if (self.messages.count != 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
    
    [self.view layoutIfNeeded];
}

/**发送SOS紧急消息操作*/
- (void)operationOfSendSOSMessage:(UIImage *)image latitudeL:(double)latitude longitude:(double)longtitude address:(NSString *)addresss
{
    WEAKSELF
    //构造图片消息+SOS扩展字段
    GPMessageItem *messageItem = [self sendSOSMessage:image latitude:latitude longitude:longtitude address:addresss to:self.chatViewModel.toId conversationId:self.chatViewModel.conversationId chatType:EMChatTypeChat progress:nil completion:^(GPMessageItem *messageItem, EMMessage *message, EMError *error) {
        
        NSLog(@">>>>>>>>>>smallyou:messsageItem=%zd,,,,message=%zd",messageItem.message.status,message.status);
        //不管成功与否，刷新表格（消息的发送状态SDK已经自动更新）
        [weakSelf.tableView reloadData];
        
    }];
    
    //更新数据源
    [self messageArray:self.messages addMessage:messageItem];
    [self.tableView reloadData];
    [self gp_scrollTableViewToBottom:NO];
    if (self.messages.count != 0) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.messages.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
    }
    
    [self.view layoutIfNeeded];
}


#pragma mark - GMSPlacePickerViewControllerDelegate 谷歌地点选择器协议
- (void)placePicker:(GMSPlacePickerViewController *)viewController didPickPlace:(GMSPlace *)place {
    // Dismiss the place picker, as it cannot dismiss itself.
    [viewController dismissViewControllerAnimated:YES completion:nil];
    
    //选定了地点，则发送当前地理位置
    [self operationOfSendLocationMessage:place.coordinate.latitude longitude:place.coordinate.longitude address:place.formattedAddress name:place.name];
    
}

- (void)placePickerDidCancel:(GMSPlacePickerViewController *)viewController {
    // Dismiss the place picker, as it cannot dismiss itself.
    [viewController dismissViewControllerAnimated:YES completion:nil];
    
    NSLog(@"No place selected");
}


#pragma mark - RecordingDelegate
#pragma mark - RecordingDelegate --录音相关回调
/**录音变化回调*/
- (void)recorderManager:(RecorderManager *)recorderManager levelMeterChanged:(float)levelMeter
{
    NSLog(@"----->音量变化 %f", levelMeter);
}

/**结束录音*/
- (void)recorderManager:(RecorderManager *)recorderManager didFinishedWithFileName:(NSString *)filePath time:(NSTimeInterval)interval
{
    //如果时间太短
    if ((int)interval <= 0) {
        
        return;
    }
    
    //取出文件名称
    NSString *fileName = filePath.lastPathComponent;
    fileName = [fileName componentsSeparatedByString:@"."].firstObject;
    
    //发送语音
    [self operationOfSendVoiceMessage:filePath fileName:fileName duration:interval];
}


@end
